import {defineStore} from 'pinia'
import {ref} from "vue";
import {reqGetRoutes} from "@/api/modules/route.js"
import router from "@/router/index.js";
import {anyRoutes} from "@/router/routes.js";
const modules=import.meta.glob("../../views/**/**.vue")

export const useRouteStore = defineStore("route", () => {
    const routes = ref()
    const getRoutes = async (accountId) => {
        const res = await reqGetRoutes(accountId)
        routes.value = res[0].children
        const tempRoutes = JSON.parse(JSON.stringify(routes.value))
        handleDynamicRoutes(tempRoutes)
        tempRoutes.forEach(item => {
            router.addRoute(item)
        })
        router.addRoute(...anyRoutes)
    }
    return {
        routes,
        getRoutes
    }
})
// const keys=Object.keys(modules)
const map=new Map();
Object.keys(modules).forEach(key=>{
    map.set(key,modules[key])
})
function handleDynamicRoutes(routes) {
    for (let route of routes) {
        if (route.componentPath){
            route.component = map.get("../.." + route.componentPath+".vue")
        }
        route.meta = {label: ''}
        route.meta.label = route.label
        if (route.children && route.children.length > 0) {
            handleDynamicRoutes(route.children)
        }
    }
}
